// PipePersist.cpp: implementation of the PipePersist class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PipePersist.h"
#include "Pipe.h"
#include "PipeTable.h"
#include "DatabaseBase.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace PDMS::PDMSDBIF;
using namespace PDMS::PDMSFrame;
using namespace PDMS::PDMSDomain;
 
BEGIN_REGISTER_MAP( PipePersist, PersistRegBase )
REGISTER( UE_RUNTIME_CLASS( Pipe ), GET_REGISTER( PersistRegBase, VersionOne ) )
END_REGISTER_MAP();

PipePersist::PipePersist()
{

}

PipePersist::~PipePersist()
{

}

UESoft::ErrorStatus PipePersist::Update( Persistable* pObj )
{
	if ( NULL == pObj)
	{
		return UESoft::eNullObjectPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Pipe ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Pipe* pPipe = static_cast<Pipe*>( pObj );

	DatabaseBase* pBase = GetDatabase();
	DatabaseElement* pBaseElement;
	pBaseElement = pBase->GetDatabaseElement( _T("PipeData") );
	ASSERT( pBaseElement != NULL );

	PipeTable* pPipeTable = static_cast<PipeTable*>( pBaseElement );
	UESoft::ErrorStatus es = pPipeTable->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pPipeTable->UpdateRecord( pPipe->Id().ObjectID() );
	pPipeTable->SetBuildFlag( pPipe->GetBuiltValue() );
	pPipeTable->SetCaseRef( pPipe->GetCasrefValue() );
	pPipeTable->SetDescription( pPipe->GetDscpValue() );
	pPipeTable->SetFluRef( pPipe->GetFlurefValue() );
	pPipeTable->SetFunction( pPipe->GetFucValue() );
	pPipeTable->SetID( pPipe->Id().ObjectID() );
	pPipeTable->SetIspec( pPipe->GetIspecValue() );
	pPipeTable->SetMatRef( pPipe->GetMatrefValue() );
	pPipeTable->SetParentID( pPipe->OwnerId().ObjectID() );
	pPipeTable->SetPipeBore( pPipe->GetBoreValue() );
	pPipeTable->SetPressure( pPipe->GetPresValue() );
	pPipeTable->SetPspec( pPipe->GetPspecValue() );
	pPipeTable->SetPtspec( pPipe->GetPaintRef() );
	pPipeTable->SetPurpose( pPipe->GetPurpValue() );
	pPipeTable->SetShopFlag( pPipe->GetShopValue() );
	pPipeTable->SetTemperature( pPipe->GetTempValue() );
	pPipeTable->SetTspec( pPipe->GetTspecValue() );

	return UESoft::eOk;
}

UESoft::ErrorStatus PipePersist::Retrieve( Persistable*& pObj )
{
	UESoft::ErrorStatus es = ReadPaternity( pObj );
	if ( UESoft::eOk != es )
	{
		return es;
	}
	
	if ( NULL == pObj)
	{
		return UESoft::eNullObjectPointer;
	}

	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Pipe ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}

	Pipe* pPipe = static_cast<Pipe*>( pObj );

	DatabaseBase* pBase = GetDatabase();
	DatabaseElement* pBaseElement;
	pBaseElement = pBase->GetDatabaseElement( _T("PipeData") );
	ASSERT( pBaseElement != NULL );

	StdString strCondition;
	strCondition.Format( _T("ID = %ld"), pObj->Id().ObjectID() );

	PipeTable* pPipeTable = static_cast<PipeTable*>( pBaseElement );
	es = pPipeTable->Open( strCondition );
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pPipe->SetBuildValue( pPipeTable->GetBuildFlag() );
	pPipe->SetCasrefValue( pPipeTable->GetCaseRef() );
	pPipe->SetDscpValue( pPipeTable->GetDescription() );
	pPipe->SetFlurefValue( pPipeTable->GetFluRef() );
	pPipe->SetFucValue( pPipeTable->GetFunction());
	pPipe->SetIspecValue( pPipeTable->GetIspec() );
	pPipe->SetMatrefValue( pPipeTable->GetMatRef() );
	pPipe->OwnerId( ObjectId(pPipe->Id().EnviromentID(), pPipeTable->GetParentID()) );
	pPipe->SetBoreValue( pPipeTable->GetPipeBore() );
	pPipe->SetPresValue( pPipeTable->GetPressure() );
	pPipe->SetPspecValue( pPipeTable->GetPspec() );
	pPipe->SetPaintRef( pPipeTable->GetPtspec() );
	pPipe->SetPurpValue( pPipeTable->GetPurpose() );
	pPipe->SetShopValue( pPipeTable->GetShopFlag() );
	pPipe->SetTempValue( pPipeTable->GetTemperature() );
	pPipe->SetTspecValue( pPipeTable->GetTspec() );

	return UESoft::eOk;
}

UESoft::ErrorStatus PipePersist::Create( Persistable* pObj )
{
	UESoft::ErrorStatus es = CreatePaternity( pObj );
	if ( UESoft::eOk != es)
	{
		return es;
	}

	if ( NULL == pObj)
	{
		return UESoft::eNullObjectPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Pipe ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Pipe* pPipe = static_cast<Pipe*>( pObj );

	DatabaseBase* pBase = GetDatabase();
	DatabaseElement* pBaseElement;
	pBaseElement = pBase->GetDatabaseElement( _T("PipeData") );
	ASSERT( pBaseElement != NULL );

	PipeTable* pPipeTable = static_cast<PipeTable*>( pBaseElement );
	es = pPipeTable->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pPipeTable->NewRecord();
	pPipeTable->SetBuildFlag( pPipe->GetBuiltValue() );
	pPipeTable->SetCaseRef( pPipe->GetCasrefValue() );
	pPipeTable->SetDescription( pPipe->GetDscpValue() );
	pPipeTable->SetFluRef( pPipe->GetFlurefValue() );
	pPipeTable->SetFunction( pPipe->GetFucValue() );
	pPipeTable->SetID( pPipe->Id().ObjectID() );
	pPipeTable->SetIspec( pPipe->GetIspecValue() );
	pPipeTable->SetMatRef( pPipe->GetMatrefValue() );
	pPipeTable->SetParentID( pPipe->OwnerId().ObjectID() );
	pPipeTable->SetPipeBore( pPipe->GetBoreValue() );
	pPipeTable->SetPressure( pPipe->GetPresValue() );
	pPipeTable->SetPspec( pPipe->GetPspecValue() );
	pPipeTable->SetPtspec( pPipe->GetPaintRef() );
	pPipeTable->SetPurpose( pPipe->GetPurpValue() );
	pPipeTable->SetShopFlag( pPipe->GetShopValue() );
	pPipeTable->SetTemperature( pPipe->GetTempValue() );
	pPipeTable->SetTspec( pPipe->GetTspecValue() );

	return UESoft::eOk;
}